<template>
  <div class="app-container">
    <div class="title">
      <div class="title-name">{{$route.meta.title}}</div>
      <div class="title-icon">
        <i class="iconfont iconhelp" @click="preview"></i>
      </div>
    </div>
    <!-- <a-title /> -->
    <el-form ref="queryForm" :model="queryForm" :inline="true" label-width="128px">
        <!-- <el-form-item label="状态" prop="enableFlag">
          <el-select v-model="queryForm.enableFlag" placeholder="请选择">
            <el-option v-for="item in enableFlagData" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>-->
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="queryForm.name" placeholder="请输入" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="项目代码" prop="code">
          <el-input v-model="queryForm.code" placeholder="请输入" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="项目执行部门" prop="executeDept">
          <el-input v-model="queryForm.executeDept" placeholder="请输入" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="项目负责人" prop="projectLeaderName">
          <el-select v-model="queryForm.projectLeaderName" placeholder="请选择" style="width:100%;" :filterable="true">
            <el-option v-for="item in userList" :key="item.userId" :label="item.nickName" :value="item.nickName">
              <span style="float: left">{{ item.userName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.nickName }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目负责人代码" prop="projectLeaderNo">
          <el-input v-model="queryForm.projectLeaderNo" placeholder="请输入" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="冻结状态" prop="freezeStatus">
          <el-select v-model="queryForm.freezeStatus" placeholder="请选择">
            <el-option v-for="item in isFreezeData" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="项目所在公司" prop="projectCompany">
          <el-input v-model="queryForm.projectCompany" placeholder="请输入" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="button"
          class="el-button el-button--primary el-button--mini"
          icon="el-icon-refresh-right"
          size="mini"
          @click="handleSynchronize"
        >全局同步</el-button>
      </el-col>
    </el-row>


    <el-table stripe v-loading="loading" :data="dataList" :default-sort="{prop: 'code', order: 'ascending'}" @sort-change="tableSortChange">
      <el-table-column label="项目名称" align="left" prop="name" :sort-orders="['ascending', 'descending']" sortable="custom" />
      <el-table-column label="项目代码" align="center" prop="code" :sort-orders="['ascending', 'descending']" sortable="custom" />
      <el-table-column label="项目执行部门" align="center" prop="executeDept" :sort-orders="['ascending', 'descending']" sortable="custom" />
      <el-table-column label="项目负责人" align="center" prop="projectLeaderName" :sort-orders="['ascending', 'descending']" sortable="custom" />
      <el-table-column label="项目负责人代码" align="center" prop="projectLeaderNo" :sort-orders="['ascending', 'descending']" sortable="custom" />
      <el-table-column label="项目所在公司" align="center" prop="projectCompany" :sort-orders="['ascending', 'descending']" sortable="custom" />
      <el-table-column label="数据有效性" align="center" prop="isvalid" :sort-orders="['ascending', 'descending']" sortable="custom">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isvalid === '1' ? 'success' : 'danger'" disable-transitions>{{ scope.row.isvalid =='1'?'有效':'作废' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="冻结状态" align="center" prop="freezeStatus" :sort-orders="['ascending', 'descending']" sortable="custom">
        <template slot-scope="scope">
          <el-tag :type="scope.row.freezeStatus === '1' ? 'success' : 'danger'" disable-transitions>{{ scope.row.freezeStatus =='1'?'正常':'冻结' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" prop="enableFlag" :sort-orders="['ascending', 'descending']" sortable="custom">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.enableFlag === '0'" type="success" disable-transitions> 启用 </el-tag>
          <el-tag v-else type="warning" disable-transitions> 未启用 </el-tag>

        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermi="['system:project:edit']" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="text" size="mini" :icon="scope.row.isvalid =='1'?'el-icon-open':'el-icon-turn-off'" @click="handleThaw(scope.row)">{{ scope.row.freezeStatus =='1'?'项目冻结':'项目解冻' }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryForm.pageNum" :limit.sync="queryForm.pageSize" @pagination="getList" />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :visible.sync="open" width="850px" :show-close="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="项目名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple-light">
              <el-form-item label="项目代码" prop="code">
                <el-input v-model="form.code" placeholder="请输入" />
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="项目负责人" prop="projectLeaderName">
                <el-select v-model="form.projectLeader" placeholder="请选择" style="width:100%;" :filterable="true">
                  <el-option v-for="item in userList" :key="item.userId" :label="item.nickName" :value="item.userId">
                    <span style="float: left">{{ item.userName }}</span>
                    <span style="float: right; color: #8492a6; font-size: 13px">{{ item.nickName }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple-light">
              <el-form-item label="项目负责人代码" prop="projectLeaderNo">
                <el-input v-model="form.projectLeaderNo" placeholder="请输入" />
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="项目所在公司" prop="projectCompany">
                <el-input v-model="form.projectCompany" placeholder="请输入" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple-light">
              <el-form-item label="项目执行部门" prop="executeDept">
                <el-input v-model="form.executeDept" placeholder="请输入" />
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="数据有效性" prop="isvalid">
              <el-select v-model="form.isvalid" placeholder="请选择" style="width:100%;">
                <el-option v-for="item in isValidData" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 预览 -->
    <el-dialog :title="title" :visible.sync="openPreview" width="80%">
      <iframe :src="manualPreviewUrl" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>

<script>
import {
  systemProjectList,
  getProject,
  updateProject,
  globalSynchronize
} from '@/api/system/systemProject';
import { userAllList } from '@/api/system/user';
import aTitle from '@/components/Title';

export default {
  name: 'Freeze',
  components: {
    aTitle
  },
  data() {
    return {
      manualPreviewUrl: null,
      openPreview: false,
      guid: '',
      enableFlagData: [
        {
          value: '0',
          label: '正常'
        },
        {
          value: '1',
          label: '冻结'
        }
      ],
      isValidData: [
        {
          value: '0',
          label: '作废'
        },
        {
          value: '1',
          label: '有效'
        }
      ],
      isFreezeData: [
        {
          value: '0',
          label: '冻结'
        },
        {
          value: '1',
          label: '正常'
        }
      ],
      // 查询参数
      queryForm: {
        pageNum: 1,
        pageSize: 10,
        enableFlag: undefined,
        isvalid: undefined,
        name: '',
        code: '',
        executeDept: '',
        projectLeaderName: '',
        projectLeaderNo: '',
        projectCompany: ''
      },
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 任务模板表格数据
      dataList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      userList: []
    }
  },
  created() {
    this.getList()
    userAllList().then(response => {
      this.userList = response.data
    })
  },
  methods: {
    tableSortChange(column) {
      this.queryForm.pageNum = 1
      if (column.order === 'descending') {
        this.queryForm.sortBy = column.prop
        this.queryForm.order = 'desc';
      } else {
        this.queryForm.sortBy = column.prop
        this.queryForm.order = 'asc';
      }
      this.getList()
    },
    // 项目解冻操作
    handleThaw(row) {
      const guid = row.guid
      let freezeStatus = row.freezeStatus == '1' ? '0' : '1';
      this.$confirm('是否操作此项目?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          updateProject({ guid, freezeStatus }).then(response => {
            if (response.code === 200) {
              if(freezeStatus =='0'){
                this.msgSuccess('冻结成功！')
              }else{
                this.msgSuccess('解冻成功！')
              }
              this.getList()
            } else {
              this.msgError(response.msg)
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.open = true
      this.guid = row.guid
      getProject(this.guid).then(res => {
        if (res.code == 200) {
          this.form = res.data
        } else {
          this.msgError(res.msg)
        }
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          updateProject(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            } else {
              this.msgError(response.msg)
            }
          })
        }
      })
    },
    /** 查询任务列表 */
    getList() {
      this.loading = false
      systemProjectList(this.queryForm).then(response => {
        this.dataList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryForm.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        guid: undefined
      }
      this.resetForm('form')
    },
    preview () {
      this.openPreview = true;
      this.manualPreviewUrl = `${this.previewUrl}${encodeURIComponent(
        `${this.operationManual}业务标准库/项目冻结解冻.xlsx`
      )}&name=${encodeURIComponent("项目冻结解冻.xlsx")}`;
    },
    //全局同步-同步项目负责人、项目、项目合同所有数据
    handleSynchronize(){
      let vm = this;
      this.$confirm('是否确认从经营系统同步同步项目负责人、项目、项目合同所有数据?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function () {
          vm.$loading({ lock: true, text: '同步中', background: 'rgba(0, 0, 0, 0.7)', spinner: 'el-icon-loading', })
          return globalSynchronize()
        })
        .then(res => {
          if (res.code === 200) {
            this.msgSuccess("同步成功");
            this.getList()
            vm.$loading({ lock: true, text: '同步中', background: 'rgba(0, 0, 0, 0.7)', spinner: 'el-icon-loading', }).close();
          }
        })
        .catch(function () { })
    }
  }
}
</script>
<style lang="scss" scoped>
.title {
  height: 30px;
  width: 100%;
  display: flex;
  // align-items: center;
  border-bottom: 1px solid #dfe6ec;
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 20px;
  .title-name {
    width: 200px;
  }
  .title-icon {
    flex: 1;
    display: flex;
    justify-content: flex-end;
    cursor: pointer;
    .iconhelp {
      font-size: 18px;
    }
  }
}
</style>
